#include "OnSpaceState.h"
#include "Player.h"
#include <math.h>
#include "ShareComponents.h"
#include "FWKeyboard.h"
#include "RSManager.h"
#include "BulletManager.h"
#define CONTRA_VX 2.4f 

OnSpaceState::OnSpaceState(Player* player)
{
	p = player;
	this->_sJumpLeft = RSManager::getInstance()->getSprite(PLAYER_JUMP_UP_LEFT);
	this->_sJumpRight = RSManager::getInstance()->getSprite(PLAYER_JUMP_UP_RIGHT);
	this->_sJumpDownLeft = RSManager::getInstance()->getSprite(PLAYER_FALL_DOWN_LEFT);
	this->_sJumpDownRight = RSManager::getInstance()->getSprite(PLAYER_FALL_DOWN_RIGHT);
	gDirection = RIGHT;
	jDirection = UP;//
	p->setSprite(_sJumpDownRight);
}


OnSpaceState::~OnSpaceState(void)
{
}

PLAYER_STATE OnSpaceState::handleInput()
{
	p->set_vx(0);

	KBState = 0;
	if (gl_keyboard->isKeyDown(DIK_D))	
	{
		KBState = KBState | D;	
		p->set_vx(CONTRA_VX);
		gDirection = RIGHT;
		if(jDirection == UP)
			p->setSprite(_sJumpRight);
		else
			p->setSprite(_sJumpDownRight);
	}
	if (gl_keyboard->isKeyDown(DIK_W))	
		KBState = KBState | W;					
	if (gl_keyboard->isKeyDown(DIK_A))	
	{
		KBState = KBState | A;	
		p->set_vx(-CONTRA_VX);
		gDirection = LEFT;
		if(jDirection == UP)
			p->setSprite(_sJumpLeft);
		else
			p->setSprite(_sJumpDownLeft);
	}
	if (gl_keyboard->isKeyDown(DIK_S))	
		KBState = KBState | S;					

	if(p->getBulletID() == B_M)
	{
		if(gl_keyboard->isKeyDown(DIK_I))		
			KBState = KBState | I;		
	}
	else
	{
		if(gl_keyboard->isKeyPressed(DIK_I))		
			KBState = KBState | I;			
	}

	switch (KBState)
	{	
	case I:
		if(gDirection == LEFT)
			p->shoot(0, 19, M_PI);
		else
			p->shoot(38, 19, 0);
		break;
	case DI:		
		if(jDirection == UP)
		{
			p->setSprite(_sJumpRight);
			p->shoot(38, 20, 0);
		}
		else
		{
			p->setSprite(_sJumpDownRight);
			p->shoot(0, 19, 0);
		}
		gDirection = RIGHT;
		break;
	case AI:		
		if(jDirection == UP)
		{
			p->setSprite(_sJumpLeft);
			p->shoot(0, 20, M_PI);
		}
		else
		{
			p->setSprite(_sJumpDownLeft);
			p->shoot(0, 19, M_PI);
		}
		gDirection = LEFT;
		break;
	case WI:		
		if(gDirection == LEFT)
		{			
			if (jDirection == UP)							
			{
				p->setSprite(_sJumpLeft);
				p->shoot(20, 20, M_PI_2);
			}			
			else							
			{
				p->setSprite(_sJumpDownLeft);
				p->shoot(0, 40, M_PI_2);	
			}		
		}
		else
		{
			
			if (jDirection == UP)						
			{
				p->setSprite(_sJumpRight);
				p->shoot(20, 20, M_PI_2);			
			}
			else							
			{
				p->setSprite(_sJumpDownRight);
				p->shoot(0, 40, M_PI_2);			
			}
		}
		break;
	case SI:
		if(gDirection == LEFT)
		{			
			if (jDirection == UP)							
			{
				p->setSprite(_sJumpLeft);
				p->shoot(20, 20, -M_PI_2);
			}			
			else							
			{
				p->setSprite(_sJumpDownLeft);
				p->shoot(0, 40, -M_PI_2);	
			}		
		}
		else
		{
			if (jDirection == UP)						
			{
				p->setSprite(_sJumpRight);
				p->shoot(20, 20, -M_PI_2);			
			}
			else							
			{
				p->setSprite(_sJumpDownRight);
				p->shoot(20, 40, -M_PI_2);			
			}
		}
		break;
	case ASI:		
		gDirection = LEFT;
		if(jDirection == UP)
		{
			p->setSprite(_sJumpLeft);
			p->shoot(0, 0, 5*M_PI_4);
		}
		else
		{
			p->setSprite(_sJumpDownLeft);
			p->shoot(0, 34, 5*M_PI_4);
		}
		break;
	case DSI:		
		gDirection = RIGHT;
		if(jDirection == UP)
		{
			p->setSprite(_sJumpRight);
			p->shoot(38, 0, -M_PI_4);
		}
		else
		{
			p->setSprite(_sJumpDownRight);
			p->shoot(48, 34, -M_PI_4);
		}
		break;
	case DWI:
		gDirection = RIGHT;
		if(jDirection == UP)
		{
			p->setSprite(_sJumpRight);
			p->shoot(38, 38, M_PI_4);
		}
		else
		{
			p->setSprite(_sJumpDownRight);
			p->shoot(48, 34, M_PI_4);
		}
		break;
	case AWI:
		gDirection = LEFT;
		if(jDirection == UP)
		{
			p->setSprite(_sJumpRight);
			p->shoot(0, 38, 3*M_PI_4);
		}
		else
		{
			p->setSprite(_sJumpDownRight);
			p->shoot(0, 34, 3*M_PI_4);
		}
		break;
	default:
		if(jDirection == UP)
			if(gDirection == LEFT)
				p->setSprite(_sJumpLeft);
			else
				p->setSprite(_sJumpRight);
		else
			if(gDirection == LEFT)
				p->setSprite(_sJumpDownLeft);
			else
				p->setSprite(_sJumpDownRight);
		break;
	}
	return ONSPACE;
}

void OnSpaceState::jump(JUMP_DIRECTION jumpDirection, GO_DIRECTION goDirection)
{
	if(jumpDirection == UP)	
		p->jumpUp();		
	else
		p->jumpDown();
	jDirection = jumpDirection;
	gDirection = goDirection;
}